package com.ruoyi.yipaiju.utils;


import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.io.image.ImageData;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.draw.SolidLine;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.borders.Border;
import com.itextpdf.layout.element.*;
import com.itextpdf.layout.properties.HorizontalAlignment;
import com.itextpdf.layout.properties.TextAlignment;
import com.itextpdf.layout.properties.UnitValue;
import com.ruoyi.common.enums.DueDiligenceReportEnums;
import com.ruoyi.yipaiju.domain.vo.CommonEntityVO;
import com.ruoyi.yipaiju.domain.vo.ReportResultVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

public class DueDiligenceReportGeneratorBak {
    // 定义图片相关的正则和扩展名
    private static final Pattern WEB_IMAGE_PATTERN = Pattern.compile("^https?://.*\\.(jpg|jpeg|png|gif|bmp|webp)$", Pattern.CASE_INSENSITIVE);
    private static final List<String> IMAGE_EXTENSIONS = Arrays.asList(".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp");
    private static final Logger log = LoggerFactory.getLogger(DueDiligenceReportGeneratorBak.class);

    public static void createPdf(String dest, Map<String, ReportResultVO> map) throws IOException {
        try {
            // 初始化文档
            PdfWriter writer = new PdfWriter(dest);
            PdfDocument pdf = new PdfDocument(writer);
            Document document = new Document(pdf, PageSize.A4);
            //加载中文字体（使用系统字体或提供字体文件）

            String fontPath;
            String osName = System.getProperty("os.name").toLowerCase();

            if (osName.contains("win")) {
                fontPath = "C:/Windows/Fonts/simhei.ttf";
            } else {
                fontPath = "/usr/share/fonts/my_fonts/simhei.ttf";
            }

            PdfFont chineseFont = PdfFontFactory.createFont(fontPath, PdfEncodings.IDENTITY_H);
            // TODO 封面页 （接口暂未返回）
//            createCoverPage(document, chineseFont);

            //前言页             ---> frontier
            ReportResultVO frontier = map.get(DueDiligenceReportEnums.FRONTIER.getCode());
            if(frontier != null){
                createPrefacePage(document, chineseFont, frontier);
            }

            //标的物基础信息       ---> basicInfo
            ReportResultVO basicInfo = map.get(DueDiligenceReportEnums.BASICINFO.getCode());
            if(basicInfo != null){
                createBasicInfoPage(document, chineseFont, basicInfo);
            }


            //标的物拍卖信息       ---> auctionInfo
            ReportResultVO auctionInfo = map.get(DueDiligenceReportEnums.AUCTIONINFO.getCode());
            if(auctionInfo != null){
                createAuctionInfoPage(document, chineseFont, auctionInfo);
            }


            //图像信息            ---> imageInfo
            ReportResultVO imageInfo = map.get(DueDiligenceReportEnums.IMAGEINFO.getCode());
            if(imageInfo != null && imageInfo.getValues() != null){
                List<CommonEntityVO> entityVOS = imageInfo.getValues();
                for(CommonEntityVO entityVO : entityVOS){
                        // 表格内容
                        createImagePage(document, entityVO.getValue(), chineseFont); // 替换为您的图片路径

                }
            }

            //标的物周边配套信息   ---> valueResearch
            ReportResultVO valueResearch = map.get(DueDiligenceReportEnums.VALUERESEARCH.getCode());
            if(valueResearch != null){
                createSurroundingInfoPage(document, chineseFont, valueResearch);
            }


            // TODO 价格走势页
//        createPriceTrendPage(document, chineseFont);    // TODO 价格走势不知道咋出来 闹腾

            //标的物权属信息       ---> ownershipInfo
            ReportResultVO ownershipInfo = map.get(DueDiligenceReportEnums.OWNERSHIPINFO.getCode());
            if(ownershipInfo != null){
                createPropertyRightInfoPage(document, chineseFont,ownershipInfo);
            }


            //标的物费用信息       ---> costInfo
            ReportResultVO costInfo = map.get(DueDiligenceReportEnums.COSTINFO.getCode());
            if(costInfo != null){
                createCostInfoPage(document, chineseFont, costInfo);
            }


            //标的物风险信息       ---> riskInfo
            ReportResultVO riskInfo = map.get(DueDiligenceReportEnums.RISKINFO.getCode());
            if(riskInfo != null){
                createRiskInfoPage(document, chineseFont, riskInfo);
            }

            document.close();
        }catch (Exception e){
            e.printStackTrace();
            log.info("生成pdf异常：{}", e.getMessage());
        }
    }

    /**
     * 封面页
     */
    private static void createCoverPage(Document document, PdfFont font) {
        // 添加页眉
        addPageHeader(document, font);

        // 公司名称
//        Paragraph company = new Paragraph("广州易拍居信息服务有限公司")
//                .setFont(font)
//                .setFontSize(16)
//                .setBold()
//                .setTextAlignment(TextAlignment.CENTER)
//                .setMarginTop(50);

        // 报告标题
        Paragraph title = new Paragraph("尽职调查报告")
                .setFont(font)
                .setFontSize(50)
                .setBold()
                .setTextAlignment(TextAlignment.CENTER)
                .setMarginTop(30);

        // 标的物信息
        Paragraph subject = new Paragraph("标的物：番禺区小谷围街求真街110号13栋1202房【（2021）粤2071执恢2261号】")
                .setFont(font)
                .setFontSize(14)
                .setTextAlignment(TextAlignment.CENTER)
                .setMarginTop(30);

        // 信息分类表格
        float[] columnWidths = {250f, 250f};
        Table infoTable = new Table(columnWidths);
        infoTable.setMarginTop(100);
        infoTable.setHorizontalAlignment(HorizontalAlignment.CENTER);

        // 添加分类单元格
        infoTable.addCell(createInfoCell("标的物基础信息", font));
        infoTable.addCell(createInfoCell("标的物权属信息", font));
        infoTable.addCell(createInfoCell("标的物费用信息", font));
        infoTable.addCell(createInfoCell("标的物风险信息", font));

        // 页脚
        Paragraph footer = new Paragraph("广州易拍居信息服务有限公司")
                .setFont(font)
                .setFontSize(14)
                .setBold()
                .setTextAlignment(TextAlignment.CENTER)
                .setFixedPosition(40, 30,
                        document.getPdfDocument().getDefaultPageSize().getWidth() - 80);

//        document.add(company);
        createImageLogo(document, "D://微信截图_20250730151444.png");
        document.add(title);
        document.add(subject);
        document.add(infoTable);
        document.add(footer);

        document.add(new AreaBreak());
    }

    /**
     * 前言页
     */
    private static void createPrefacePage(Document document, PdfFont font,ReportResultVO frontier) {
        // 添加页眉
        addPageHeader(document, font);
        Paragraph title = new Paragraph(frontier.getComment())
                .setFont(font)
                .setFontSize(20)
                .setBold()
                .setTextAlignment(TextAlignment.CENTER)
                .setMarginTop(30);
        Paragraph content = new Paragraph()
                .setFont(font)
                .setFontSize(14)
                .setTextAlignment(TextAlignment.LEFT)
                .add(frontier.getValue()).setMargin(20);
        // 前面的空格加不上 妈蛋的
        Paragraph copyright = new Paragraph()
                .setFont(font)
                .setFontSize(14)
                .setTextAlignment(TextAlignment.CENTER)
                .add("本尽职调查报告所有内容最终解释权归广州易拍居信息服务有限公司所有");
        document.add(title);
        document.add(content);
        document.add(copyright);
        document.add(new AreaBreak());
    }

    /**
     * 标的物基础信息
     */
    private static void createBasicInfoPage(Document document, PdfFont font, ReportResultVO basicInfo) {
        // 添加页眉
        addPageHeader(document, font);
        Paragraph title = new Paragraph("一、标的物基础信息")
                .setFont(font)
                .setFontSize(18)
                .setBold()
                .setTextAlignment(TextAlignment.CENTER);
        Paragraph subTitle = new Paragraph("（一）标的物基础信息表格")
                .setFont(font)
                .setFontSize(14)
                .setBold()
                .setTextAlignment(TextAlignment.LEFT)
                .setMarginTop(10);

        // 创建基础信息表格
        float[] columnWidths = {150f, 300f};
        Table table = new Table(columnWidths);
        // 表头
        Cell header = new Cell(1, 2).add(new Paragraph(basicInfo.getComment()).setFont(font).setBold());
        table.addHeaderCell(header);

        List<CommonEntityVO> entityVOS = basicInfo.getValues();
        for(CommonEntityVO entityVO : entityVOS){
            // 表格内容
            addTableRow(table, font, entityVO.getComment(), entityVO.getValue());
        }
        document.add(title);
        document.add(subTitle);
        document.add(table);
        document.add(new AreaBreak());
    }

    /**
     * 标的物拍卖信息
     */
    private static void createAuctionInfoPage(Document document, PdfFont font, ReportResultVO auctionInfo) {
        // 添加页眉
        addPageHeader(document, font);
        Paragraph subTitle = new Paragraph("（二）"+ auctionInfo.getComment())
                .setFont(font)
                .setFontSize(14)
                .setBold()
                .setTextAlignment(TextAlignment.LEFT);
        // 创建拍卖信息表格
        float[] columnWidths = {150f, 300f};
        Table table = new Table(columnWidths);
        // 表头
        Cell header = new Cell(1, 2).add(new Paragraph(auctionInfo.getComment()).setFont(font).setBold());
        table.addHeaderCell(header);
        List<CommonEntityVO> entityVOS = auctionInfo.getValues();
        for(CommonEntityVO entityVO : entityVOS){
            // 表格内容
            addTableRow(table, font, entityVO.getComment(), entityVO.getValue());
        }
        Paragraph imgSubTitle = new Paragraph("（三）标的物图片信息")
                .setFont(font)
                .setFontSize(14)
                .setBold()
                .setTextAlignment(TextAlignment.LEFT)
                .setMarginTop(20);

        document.add(subTitle);
        document.add(table);
        document.add(imgSubTitle);
        document.add(new AreaBreak());
    }

    /**
     * 图像信息
     */
    private static void createImagePage(Document document, String imagePath, PdfFont font) {
        // 添加页眉
        addPageHeader(document, font);
        try {
            // 加载图片
            ImageData imageData = ImageDataFactory.create(imagePath);
            Image image = new Image(imageData);
            // 设置图片大小（保持比例，宽度为页面宽度的80%）
            float pageWidth = document.getPdfDocument().getDefaultPageSize().getWidth() - 80;
            image.scaleToFit(pageWidth * 0.9f, Float.MAX_VALUE);
            // 居中显示
            image.setHorizontalAlignment(HorizontalAlignment.CENTER);
            image.setMarginTop(5);
            document.add(image);
        } catch (IOException e) {
            // 如果图片加载失败，添加错误提示
            document.add(new Paragraph("无法加载图片: " + imagePath)
                    .setFont(document.getPdfDocument().getDefaultFont())
                    .setFontColor(ColorConstants.RED));
        }
        document.add(new AreaBreak());
    }

    /**
     * 标的物周边配套信息
     */
    private static void createSurroundingInfoPage(Document document, PdfFont font,  ReportResultVO valueResearch) {
        addPageHeader(document, font);

        Paragraph subTitle = new Paragraph("（四）"+valueResearch.getComment())
                .setFont(font)
                .setFontSize(14)
                .setBold()
                .setTextAlignment(TextAlignment.LEFT);

        Paragraph tableTitle = new Paragraph("标的物周边配套信息表")
                .setFont(font)
                .setFontSize(16)
                .setBold()
                .setTextAlignment(TextAlignment.CENTER)
                .setMarginTop(10);

        // 创建周边配套表格
        float[] columnWidths = {100f, 350f};
        Table table = new Table(columnWidths);

        List<CommonEntityVO> entityVOS = valueResearch.getValues();
        for(CommonEntityVO entityVO : entityVOS){
            // 表格内容
            addTableRow(table, font, entityVO.getComment(), entityVO.getValue());
        }

        Paragraph priceTitle = new Paragraph("价格走势")
                .setFont(font)
                .setFontSize(14)
                .setBold()
                .setTextAlignment(TextAlignment.LEFT)
                .setMarginTop(20);

        Paragraph priceSubTitle = new Paragraph("放盘价格概况")
                .setFont(font)
                .setFontSize(14)
                .setBold()
                .setTextAlignment(TextAlignment.LEFT)
                .setMarginBottom(20);

        document.add(subTitle);
        document.add(tableTitle);
        document.add(table);
        document.add(priceTitle);
        document.add(priceSubTitle);


        document.add(new AreaBreak());
    }

    /**
     * 价格走势页
     */
    private void createPriceTrendPage(Document document, PdfFont font) {
        addPageHeader(document, font);

        Paragraph communityTitle = new Paragraph("星汇文华 大学城")
                .setFont(font)
                .setFontSize(16)
                .setBold()
                .setTextAlignment(TextAlignment.LEFT);

        Paragraph liveText = new Paragraph("直播看房 随时随地 想看就看    立即观看")
                .setFont(font)
                .setFontSize(12)
                .setTextAlignment(TextAlignment.LEFT)
                .setMarginBottom(20);

        Paragraph ranking = new Paragraph("大学城人气小区榜第2名")
                .setFont(font)
                .setFontSize(12)
                .setBold()
                .setTextAlignment(TextAlignment.LEFT);

        // 创建价格和房源数量表格
        float[] priceColumnWidths = {100f, 100f, 100f, 100f};
        Table infoTable = new Table(priceColumnWidths);
        infoTable.setWidth(UnitValue.createPercentValue(60));
        infoTable.setHorizontalAlignment(HorizontalAlignment.LEFT);

        infoTable.addCell(new Cell().add(new Paragraph("71951元/平").setFont(font).setBold()).setBorder(Border.NO_BORDER));
        infoTable.addCell(new Cell().add(new Paragraph("比上月▲5.38%").setFont(font)).setBorder(Border.NO_BORDER));
        infoTable.addCell(new Cell().add(new Paragraph("8套").setFont(font).setBold()).setBorder(Border.NO_BORDER));
        infoTable.addCell(new Cell().add(new Paragraph("2套").setFont(font)).setBorder(Border.NO_BORDER));
        infoTable.addCell(new Cell().add(new Paragraph("01月参考均价").setFont(font)).setBorder(Border.NO_BORDER));
        infoTable.addCell(new Cell().add(new Paragraph("在售房源").setFont(font)).setBorder(Border.NO_BORDER));
        infoTable.addCell(new Cell().add(new Paragraph("在租房源").setFont(font)).setBorder(Border.NO_BORDER));

        // 房源信息
        addPropertyInfo(document, font, "过五年唯一一四房带主卫 装修保养好 视野开阔 带车位",
                "4室2厅/18㎡/8㎡/五层立文华", "满五年 VR看装修 近地铁", "1420万 78,070元/平");
        addPropertyInfo(document, font, "房子户型方正，精装修，视野开阔，保养好，少有户型。",
                "5室2厅/28㎡/8㎡/南北/星汇文华", "满五年 VR看装修 近地铁", "1600万 72,781元/平");
        addPropertyInfo(document, font, "大学城南地铁口舒适三房 主室带卫生间朝南 业主诚心出",
                "3室2厅/40㎡/东北/星汇文华", "满五年 VR看装修 近地铁", "1200万 85,173元/平");

        document.add(communityTitle);
        document.add(liveText);
        document.add(ranking);
        document.add(infoTable);

        document.add(new AreaBreak());
    }

    /**
     * 标的物权属信息
     */
    private static void createPropertyRightInfoPage(Document document, PdfFont font, ReportResultVO ownershipInfo) {
        addPageHeader(document, font);
        Paragraph title = new Paragraph("二、" + ownershipInfo.getComment())
                .setFont(font)
                .setFontSize(18)
                .setBold()
                .setTextAlignment(TextAlignment.CENTER);

        // 获取两条数据，分别对应表格和图片
        List<CommonEntityVO> entityVOS = ownershipInfo.getValues();
        CommonEntityVO propertyTable = null;
        CommonEntityVO propertyImages = null;
        for(CommonEntityVO entityVO : entityVOS){
            if("propertyImages".equals(entityVO.getCode())){
                propertyImages = entityVO;
            }
            if("propertyTable".equals(entityVO.getCode())){
                propertyTable = entityVO;
            }
        }
        document.add(title);
        if(propertyTable != null){
            // 第一条数据用于权属信息表格
            CommonEntityVO tableEntity = entityVOS.get(0);
            Paragraph subTitle = new Paragraph("（一）" + tableEntity.getComment())
                    .setFont(font)
                    .setFontSize(14)
                    .setBold()
                    .setTextAlignment(TextAlignment.LEFT)
                    .setMarginTop(10);

            // 创建权属信息表格
            float[] columnWidths = {150f, 300f};
            Table table = new Table(columnWidths);

            // 表头
            Cell header = new Cell(1, 2).add(new Paragraph(tableEntity.getComment()).setFont(font).setBold());
            table.addHeaderCell(header);

            List<CommonEntityVO> entityVOList = propertyTable.getValues();
            for(CommonEntityVO entityVO : entityVOList){
                // 表格内容
                addTableRow(table, font, entityVO.getComment(), entityVO.getValue());
            }
            document.add(subTitle);
            document.add(table);
        }
        if(propertyImages != null){
            // 第二条数据用于不动产查册图
            Paragraph chartSubTitle = new Paragraph("（二）" + propertyImages.getComment())
                    .setFont(font)
                    .setFontSize(14)
                    .setBold()
                    .setTextAlignment(TextAlignment.LEFT)
                    .setMarginTop(20);
            document.add(chartSubTitle);
            List<CommonEntityVO> entityVOList = propertyImages.getValues();
            for(CommonEntityVO entityVO : entityVOList){
                String imagePath = entityVO.getValue();
                if(imagePath != null && !"".equals(imagePath)){
                    try {
                        // 加载图片
                        ImageData imageData = ImageDataFactory.create(imagePath);
                        Image image = new Image(imageData);
                        // 设置图片大小（保持比例，宽度为页面宽度的80%）
                        float pageWidth = document.getPdfDocument().getDefaultPageSize().getWidth() - 80;
                        image.scaleToFit(pageWidth * 0.9f, Float.MAX_VALUE);
                        // 居中显示
                        image.setHorizontalAlignment(HorizontalAlignment.CENTER);
                        image.setMarginTop(5);
                        document.add(image);
                    } catch (IOException e) {
                        // 如果图片加载失败，添加错误提示
                        document.add(new Paragraph("无法加载图片: " + imagePath)
                                .setFont(document.getPdfDocument().getDefaultFont())
                                .setFontColor(ColorConstants.RED));
                    }
                }else {
                    // 如果图片加载失败，添加错误提示
                    document.add(new Paragraph("暂无图片")
                            .setFont(document.getPdfDocument().getDefaultFont())
                            .setFontColor(ColorConstants.RED));
                }

            }
        }
        document.add(new AreaBreak());
    }

    /**
     * 标的物费用信息
     */
    private static void createCostInfoPage(Document document, PdfFont font, ReportResultVO costInfo) {
        addPageHeader(document, font);

        Paragraph title = new Paragraph("三、"+costInfo.getComment())
                .setFont(font)
                .setFontSize(18)
                .setBold()
                .setTextAlignment(TextAlignment.CENTER);

        Paragraph subTitle = new Paragraph("（一）标的物税费测算表格")
                .setFont(font)
                .setFontSize(14)
                .setBold()
                .setTextAlignment(TextAlignment.LEFT)
                .setMarginTop(10);

        // 创建费用信息表格
        float[] columnWidths = {150f, 300f};
        Table table = new Table(columnWidths);

        // 表头
        Cell header = new Cell(1, 2).add(new Paragraph("1、标的物").setFont(font).setBold());
        table.addHeaderCell(header);

        List<CommonEntityVO> entityVOList = costInfo.getValues();
        for(CommonEntityVO entityVO : entityVOList){
            // 表格内容
            addTableRow(table, font, entityVO.getComment(), entityVO.getValue());
        }

        // 备注信息
        Paragraph remark = new Paragraph()
                .setFont(font)
                .setFontSize(10)
                .setTextAlignment(TextAlignment.LEFT)
                .setMarginTop(10)
                .add("备注：\n")
                .add("增值税及附加：普通住宅不满五年拍卖成交价款的5.3%，满五年免征\n")
                .add("契税：受让方首套90㎡以内为1%，90㎡以上未1.5%；二套则3%\n")
                .add("个人所得税：“转让自用满五年家庭唯一住房免征”\n")
                .add("“能提供原值凭证转让所得（计税金额-房产原值-合理费用）*20%”\n")
                .add("“不能提供原值凭证无法准确计算所得额，拍卖成交价*3%”\n\n");
//                .add("该税费核算由易拍居按照税局政策预估，实际税费以税局核税结果为准");

        Paragraph remark1 = new Paragraph()
                .setFont(font)
                .setFontSize(10)
                .setTextAlignment(TextAlignment.CENTER)
                .add(new Text("该税费核算由").setFont(font))
                .add(new Text("易拍居").setFont(font).setFontColor(ColorConstants.RED))
                .add(new Text("按照税局政策预估，实际税费以税局核税结果为准").setFont(font));

        document.add(title);
        document.add(subTitle);
        document.add(table);
        document.add(remark);
        document.add(remark1);


        document.add(new AreaBreak());
    }

    /**
     * 标的物风险信息
     */
    private static void createRiskInfoPage(Document document, PdfFont font, ReportResultVO riskInfo) {
        List<CommonEntityVO> entityVOS = riskInfo.getValues();
        CommonEntityVO basicRiskInfo = null;
        CommonEntityVO riskRating = null;
        for(CommonEntityVO entityVO : entityVOS){
            if("basicRiskInfo".equals(entityVO.getCode())){
                basicRiskInfo = entityVO;
            }
            if("riskRating".equals(entityVO.getCode())){
                riskRating = entityVO;
            }
        }
        addPageHeader(document, font);
        Paragraph title = new Paragraph("四、"+riskInfo.getComment())
                .setFont(font)
                .setFontSize(18)
                .setBold()
                .setTextAlignment(TextAlignment.CENTER);


        document.add(title);
        // 创建风险信息表格
        float[] columnWidths = {150f, 300f};
        Table table = new Table(columnWidths);
        if(basicRiskInfo != null){
            Paragraph subTitle = new Paragraph("（一）"+basicRiskInfo.getComment())
                    .setFont(font)
                    .setFontSize(14)
                    .setBold()
                    .setTextAlignment(TextAlignment.LEFT)
                    .setMarginTop(10);
            // 表头
            Cell header = new Cell(1, 2).add(new Paragraph("1、标的物1").setFont(font).setBold());
            table.addHeaderCell(header);
            List<CommonEntityVO> entityVOList = basicRiskInfo.getValues();
            for(CommonEntityVO entityVO : entityVOList){
                // 表格内容
                addTableRow(table, font, entityVO.getComment(), entityVO.getValue());
            }
            document.add(subTitle);
        }
        document.add(table);
        if(riskRating != null){
            Paragraph ratingSubTitle = new Paragraph("（二）"+riskRating.getComment())
                    .setFont(font)
                    .setFontSize(14)
                    .setBold()
                    .setTextAlignment(TextAlignment.LEFT)
                    .setMarginTop(20);

            Paragraph rating = new Paragraph("易拍居-尽职调查风控评级：A（较低风险）")
                    .setFont(font)
                    .setFontSize(12)
                    .setBold()
                    .setTextAlignment(TextAlignment.LEFT);

            // 创建评级详情表格
            float[] ratingColumnWidths = {100f, 350f};
            Table ratingTable = new Table(ratingColumnWidths);
            List<CommonEntityVO> entityVOList = riskRating.getValues();
            for(CommonEntityVO entityVO : entityVOList){
                // 表格内容
                addTableRow(ratingTable, font, entityVO.getComment(), entityVO.getValue());
            }
            document.add(ratingSubTitle);
            document.add(rating);
            document.add(ratingTable);
        }

        Paragraph surveySubTitle = new Paragraph("（三）现场调查信息")
                .setFont(font)
                .setFontSize(14)
                .setBold()
                .setTextAlignment(TextAlignment.LEFT)
                .setMarginTop(20);

        Paragraph surveyInfo = new Paragraph("物业管理严格，调查人员未能入楼调查。")
                .setFont(font)
                .setFontSize(12)
                .setTextAlignment(TextAlignment.LEFT);

        // 免责声明
        Paragraph disclaimer = new Paragraph()
                .setFont(font)
                .setFontSize(12)
                .setTextAlignment(TextAlignment.LEFT)
                .setMarginTop(20)
                .add("本《尽职调查报告》是《拍卖法》、《最高人民法院关于人民法院网络司法拍卖的规定》")
                .add("及《拍卖网司法拍卖平台服务协议》等相关法律的规定，以拍卖标的物送拍机构公示信息为基础，")
                .add("作为独立运营第三方运用互联网大数据、现场走访和相关部门咨询等方式，对相对应信息进行收集与梳理，")
                .add("最终提供必要风险提示和解决方案。");

        Paragraph disclaimer1 = new Paragraph()
                .setFont(font)
                .setFontSize(12)
                .setTextAlignment(TextAlignment.CENTER)
                .setMarginTop(20)
                .add(new Text("本尽职调查报告所有内容最终解释权归").setFont(font))
                .add(new Text("易拍居").setFont(font).setFontColor(ColorConstants.RED))
                .add(new Text("所有").setFont(font));


        document.add(surveySubTitle);
        document.add(surveyInfo);
        document.add(disclaimer);
//        createImageLogo(document, "D://微信截图_20250730151444.png");
        document.add(disclaimer1);
    }


    private static void addPageHeader(Document document, PdfFont font) {
        // 公司名称
        Paragraph company = new Paragraph("广州易拍居信息服务有限公司")
                .setFont(font)
                .setFontSize(10)
                .setTextAlignment(TextAlignment.CENTER)
                .setMarginTop(5);
        // 分隔线（正确创建方式）
        LineSeparator line = new LineSeparator(new SolidLine());
        line.setStrokeWidth(1f); // 设置线宽为1像素
        // 添加到文档
        document.add(company);
        document.add(line);
    }

    private static Cell createInfoCell(String text, PdfFont font) {
        return new Cell()
                .add(new Paragraph(text).setFont(font).setTextAlignment(TextAlignment.CENTER))
                .setHeight(60)
                .setFontSize(20)
                .setBold()
                .setBorder(Border.NO_BORDER);
    }

    private static void addTableRow(Table table, PdfFont font, String key, String value) {
        table.addCell(new Cell().add(new Paragraph(key).setFont(font)));
        table.addCell(new Cell().add(new Paragraph(value).setFont(font)));
    }

    private void addPropertyInfo(Document document, PdfFont font, String title, String info1, String info2, String price) {
        Paragraph p = new Paragraph()
                .setFont(font)
                .setMarginBottom(15);

        p.add(new Text(title + "\n").setBold());
        p.add(new Text(info1 + "\n"));
        p.add(new Text(info2 + "\n"));
        p.add(new Text(price).setBold().setUnderline());

        document.add(p);
    }

    private static void createImageLogo(Document document, String imagePath) {
        // 添加页眉
        try {
            // 加载图片
            ImageData imageData = ImageDataFactory.create(imagePath);
            Image image = new Image(imageData);
            // 设置图片大小（保持比例，宽度为页面宽度的80%）
            float pageWidth = document.getPdfDocument().getDefaultPageSize().getWidth() - 80;
            image.scaleToFit(pageWidth * 0.4f, Float.MAX_VALUE);
            // 居中显示
            image.setHorizontalAlignment(HorizontalAlignment.CENTER);
            image.setMarginTop(50);
            document.add(image);
        } catch (IOException e) {
            // 如果图片加载失败，添加错误提示
            document.add(new Paragraph("无法加载图片: " + imagePath)
                    .setFont(document.getPdfDocument().getDefaultFont())
                    .setFontColor(ColorConstants.RED));
        }
    }

    private static void createImagePlaceholderPage(Document document, PdfFont font) {
        document.add(new AreaBreak());

        // 图片占位符
        Paragraph placeholder = new Paragraph("[图片占位]")
                .setFont(font)
                .setFontSize(24)
                .setTextAlignment(TextAlignment.CENTER)
                .setMarginTop(250);
        document.add(placeholder);
        document.add(new AreaBreak());
        // 第二张图片占位页
        document.add(placeholder);
        document.add(new AreaBreak());
    }
}